/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2009-2020 Rock Mao
* creator : Rock Mao
* created : 2009-9-21   20:29
* filename: LaborObjectDepot.h
-----------------------------------------------------------------------------
*/

#ifndef __LABOROBJECTDEPOT_H__
#define __LABOROBJECTDEPOT_H__
#include "LaborCoreDef.h"
#include "LaborHashString.h"
#include "Mem/stl_dllalloc.h"
using namespace std;
NAMESPACE_LABOR3D_BEGIN

template <typename T> class TLObjectDepot
{
	typedef map<size_t , T * > vObjectTable;
	vObjectTable m_vObjTable; 
public:
	bool add(const wchar_t* name , T* object)
	{
		size_t hname = fLStringHash(name);
		return add(hname , object);
	}
	bool add(size_t  hash_name , T* object)
	{
		if( find(hash_name) ) return false;
		m_vObjTable.insert( vObjectTable::value_type(hash_name , object));
		return true;
	}

	T*   find(size_t  hash_name)
	{
		vObjectTable::iterator pos = m_vObjTable.find( hash_name );
		if(pos != m_vObjTable.end() )
			return pos->second;
		return NULL;
	}

	T*   find(const wchar_t* name)
	{
		size_t hname = fLStringHash(name);
		return find(hname);
	}

	bool remove(T* object)
	{
		vObjectTable::iterator pos = m_vObjTable.begin( );
		for(; pos != m_vObjTable.end()  ; pos ++)
		{
			if(object == pos->second )
			{
				m_vObjTable.erase(pos);
				return true;
			}
		}	
		return false;
	}

	bool remove(const wchar_t* name)
	{
		size_t hname = fLStringHash(name);
		vObjectTable::iterator pos = m_vObjTable.find( hname );
		if(pos != m_vObjTable.end()  )
		{
			m_vObjTable.erase(pos);
			return true;
		}	
		return false;
	}

	bool remove(size_t  hash_name , T* object)
	{
		vObjectTable::iterator pos = m_vObjTable.find( hash_name );
		if(pos != m_vObjTable.end()  )
		{
			if(object == NULL || object == pos->second )
			{
				m_vObjTable.erase(pos);
				return true;
			}
		}	
		return false;
	}

};

NAMESPACE_LABOR3D_END
#endif